package ils.domain.issue.model;

import ils.util.HibernateSession;
import ils.util.HibernateSessionManager;
import ils.util.SpringUtil;
import ils.util.Util;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: rejeevd
 * Date: 23/09/12
 * Time: 19:14
 * To change this template use File | Settings | File Templates.
 */
public class ReserveRepositoryTest
{
    HibernateSessionManager hibernateSessionManager = new HibernateSessionManager();
    ApplicationContext context = SpringUtil.getApplicationContext();
    ReserveRepository reserveRepository = (ReserveRepository)context.getBean("reserveRepository");

    @Test
    public void testCreateReserve()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Reserve reserve = new Reserve();
            reserve.setBookId(1);
            reserve.setUserId(3);
            reserve.setReserveDate(new Date());
            reserve.setExpiryDate(new Date(System.currentTimeMillis() + Util.DAY * 7));
            reserve.setStatus(true);
            reserve = reserveRepository.createReserve(reserve);
            Assert.assertNotNull(reserve.getId());
        }
    }

    @Test
    public void testEditReserve()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Reserve reserve = reserveRepository.getReserve(3, 1);
            Date expiryDate = reserve.getExpiryDate();
            if(expiryDate != null) expiryDate = new Date(reserve.getExpiryDate().getTime() + 345654);
            else expiryDate = new Date(System.currentTimeMillis() + 345654);
            reserve.setExpiryDate(expiryDate);
            reserve = reserveRepository.editReserve(reserve);
        }
    }

    @Test
    public void testGetReserveForGivenUserAndBook()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Reserve reserve = reserveRepository.getReserve(3, 1);
            Assert.assertNotNull(reserve);
        }
    }

    @Test
    public void testGetReservations()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Reserve> reservations = reserveRepository.getReservations(0, 20);
            Assert.assertNotSame(0, reservations.size());
        }
    }

    @Test
    public void testGetReservationsForBook()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Reserve> reservations = reserveRepository.getReservationsForBook(1, 0, 20);
            Assert.assertNotSame(0, reservations.size());
        }
    }

    @Test
    public void testGetReservationsForUser()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Reserve> reservations = reserveRepository.getReservationsForUser(3);
            Assert.assertNotSame(0, reservations.size());
        }
    }
}
